package org.wanda.pv.controller.basic;

import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.wanda.pv.framework.base.Result;
import org.wanda.pv.framework.page.Pagination;
import org.wanda.pv.model.basic.Point;
import org.wanda.pv.model.basic.Specialty;
import org.wanda.pv.model.basic.System;
import org.wanda.pv.service.basic.SystemService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author LiuXuyang
 * Created by lxy on 2018/5/10.
 */


@RestController
@RequestMapping("/basic/sysDesign")
public class SystemController {
    private final static Logger log = LoggerFactory.getLogger(SystemController.class);

    @Autowired
    Dao dao;

    @Autowired
    private SystemService systemService;

    /**
     * @api {post} /basic/sysDesign/updateLocaction 修改系统排序
     * @apiParam {String} id 系统ID
     * @apiParam {String} newLocation 新的排序位置
     * @apiGroup 设计系统
     */
    @RequestMapping(value = "/updateLocaction", method = RequestMethod.POST)
    public Object updateQuestionLocation(Long id, Integer newLocation) {
        try {
            System system = dao.fetch(System.class, id);
            if (system.getId() != null) {
                systemService.updateLocation(newLocation, system);
            } else {
                return Result.error("fail", "系统不存在！");
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Result.error("fail", "服务器内部错误");
        }
        return Result.success("success", "排序修改成功！");
    }


    /**
     * @api {post} /basic/sysDesign/add 添加设计系统
     * @apiParam {String} name 系统名称
     * @apiParam {String} specialtyId 专业ID
     * @apiGroup 设计系统
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Object add(System system) {
        try {
            Result result = systemService.add(system);
            return result;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Result.error("fali", "服务器内部错误！");
        }
    }

    /**
     * @api {post} /basic/sysDesign/del 删除设计系统
     * @apiParam {String} id 系统ID
     * @apiGroup 设计系统
     */
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    public Object del(@RequestParam("id") Long id) {
        try {
            Result result = systemService.del(id);
            return result;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Result.error("fali", "服务器内部错误！");
        }
    }

    /**
     * @api {post} /basic/sysDesign/update 修改设计系统
     * @apiParam {String} id 系统ID
     * @apiParam {String} name 系统名称
     * @apiParam {String} specialtyId 专业ID
     * @apiGroup 设计系统
     */

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Object update(System system) {
        try {
            Result result = systemService.updateSystem(system);
            return result;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Result.error("fali", "服务器内部错误！");
        }
    }


    /**
     * @api {post} /basic/sysDesign/list 分页查询所有系统
     * @apiParam {String} pageNum 当前页码(默认值为1)
     * @apiParam {String} pageSize 单页显示条数(默认值为10)
     * @apiParam {String} name 系统名称(默认值为空)
     * @apiGroup 设计系统
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public Object list(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                       @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                       @RequestParam(value = "name", defaultValue = "") String name) {
        try {
            Cnd cnd = Cnd.where("name", "like", "%" + name + "%");
            Pagination listPage = systemService.listPage(pageNum, pageSize, cnd
                    .and("delFlag", "=", "0").asc("location"));
            List<System> list = (List<System>) listPage.getList();
            for (System system : list) {
                system.setCategory(dao.fetch(Specialty.class, Cnd.where("id", "=", system.getSpecialtyId()).and("delFlag", "=", "0")));
            }
            return Result.success("success", listPage);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Result.error("fail", "服务器内部错误!");
        }
    }

    /**
     * @api {post} /basic/sysDesign/getAll 查询所有
     * @apiParam {String} name 设计系统名称
     * @apiGroup 设计系统
     */
    @RequestMapping(value = "/getAll", method = RequestMethod.POST)
    public Object getAll(@RequestParam(value = "name", defaultValue = "") String name) {
        try {
            List<System> listPage = systemService.query(Cnd.where("name", "like", "%" + name + "%")
                    .and("delFlag", "=", "0").asc("location"));
            for (System system : listPage) {
                system.setCategory(dao.fetch(Specialty.class, Cnd.where("id", "=", system.getSpecialtyId()).and("delFlag", "=", "0")));
            }
            return Result.success("success", listPage);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Result.error("fail", "服务器内部错误!");
        }

    }

    /**
     * @api {post} /basic/sysDesign/getBySCID 根据专业查询系统
     * @apiParam {String} specialtyId 专业ID
     * @apiGroup 设计系统
     */
    @RequestMapping(value = "/getBySCID", method = RequestMethod.POST)
    public Object getBySCID(Long specialtyId) {
        try {
            Map<String,Object> map=new HashMap<>();
            List<System> systems = dao.query(System.class, Cnd.where("specialty_id", "=", specialtyId).and("delFlag", "=", false).asc("location"));
            systems.forEach(system -> {
                int count=dao.count(Point.class,Cnd.where("system_id","=",system.getId()).and("delFlag", "=", false));
                system.setPointCount(count);
            });
            return Result.success("success", systems);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Result.error("fail", "服务器内部错误!");
        }
    }
}
